Final Project: Time Series Forecasting with LSTMs, Neural Networks Eng. Class

prof. Victor H. Alves Ribeiro, PPGEPS/PUCPR



Note

Chamada do exercicio em ingles vai aqui

Abstract

This project aims to predict the returns of a selected portfolio of agricultural commodities using Long Short-Term Memory (LSTM) neural networks. The study includes ablation experiments to understand the impact of different architectural choices on model performance. The results demonstrate the importance of hyperparameter tuning and regularization in time series forecasting tasks.

Introduction

The use of Artificial Neural Networks (ANNs) for time series forecasting has gained significant traction in recent years. This project focuses on utilizing LSTMs, a type of recurrent neural network capable of handling sequential data efficiently, to predict financial returns of agricultural commodities. A detailed ablation study is conducted to explore various architectural configurations and hyperparameters.

Literature Review

Time series forecasting is critical in finance and economics. Traditional models, such as ARIMA and exponential smoothing, have limitations when dealing with non-linear and complex data. Recent studies emphasize the robustness of LSTM models in capturing temporal dependencies. This project builds on existing research by applying LSTM networks to a unique dataset of agricultural commodity returns.

…see the full paper for this complete section …

Methods

  • Collecting commodity price data.
  • Calculating logarithmic returns.
  • Normalizing the data.
  • Training LSTM models with different configurations.
  • Performing grid search to optimize hyperparameters.
  • Conducting residual analysis to identify uncaptured patterns and issues like autocorrelation or heteroscedasticity.

Results discussion

Data Collection and Preprocessing

Python libs

Code
# Importing necessary libraries
import pandas as pd
import numpy as np
import yfinance as yf
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import ParameterGrid
import psutil
import time
import json
import os
import warnings
warnings.filterwarnings('ignore')

# Additional libraries for residual analysis
from statsmodels.stats.diagnostic import acorr_ljungbox, het_breuschpagan
import statsmodels.api as sm

First of all, we need to check the hardware availability:

Code
# Collecting hardware information
def get_system_info():
    system_info = {
        'CPU_cores': psutil.cpu_count(logical=True),
        'CPU_freq_MHz': psutil.cpu_freq().current,
        'Total_RAM_GB': round(psutil.virtual_memory().total / (1024 ** 3), 2),
        'Available_RAM_GB': round(psutil.virtual_memory().available / (1024 ** 3), 2),
        'GPU_info': 'Not available'  # Placeholder, can be expanded with libraries like GPUtil
    }
    return system_info

system_info = get_system_info()
print("System Information:", system_info)
System Information: {'CPU_cores': 12, 'CPU_freq_MHz': 1800.0, 'Total_RAM_GB': 31.69, 'Available_RAM_GB': 16.79, 'GPU_info': 'Not available'}

Loading data:

Code
# Defining the tickers
tickers = [
    "ZC=F",  # Corn Futures
    "ZW=F",  # Wheat Futures
    "KE=F",  # KC HRW Wheat Futures
    "ZR=F",  # Rough Rice Futures
    "GF=F",  # Feeder Cattle Futures
    "ZM=F",  # Soybean Meal Futures
    "ZL=F",  # Soybean Oil Futures
    "ZS=F"   # Soybean Futures
]

# Downloading price data
print("\nDownloading price data...")
data = yf.download(tickers, start="2015-01-01")['Close']

# Handling missing data
print("\nHandling missing data...")
data.fillna(method='ffill', inplace=True)  # Forward fill
data.dropna(axis=1, how='all', inplace=True)  # Drop columns with all NaNs
data.dropna(axis=0, how='any', inplace=True)  # Drop rows with any NaNs

# Verify data
print("\nData columns and their non-null counts:")
print(data.count())

if data.empty:
    print("Data is empty after cleaning. Exiting.")
    exit()

# Calculating logarithmic returns
returns = np.log(data / data.shift(1)).dropna()

# Verify returns
print("\nReturns DataFrame info:")
print(returns.info())
print(returns.head())

if returns.empty:
    print("Returns DataFrame is empty. Exiting.")
    exit()
    

returns.head() # Showing time series used (without features)

Downloading price data...
[                       0%                       ][************          25%                       ]  2 of 8 completed[******************    38%                       ]  3 of 8 completed[******************    38%                       ]  3 of 8 completed[******************    38%                       ]  3 of 8 completed[**********************75%***********            ]  6 of 8 completed[**********************88%*****************      ]  7 of 8 completed[*********************100%***********************]  8 of 8 completed

Handling missing data...

Data columns and their non-null counts:
Ticker
GF=F    2539
KE=F    2539
ZC=F    2539
ZL=F    2539
ZM=F    2539
ZR=F    2539
ZS=F    2539
ZW=F    2539
dtype: int64

Returns DataFrame info:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2538 entries, 2015-01-05 00:00:00+00:00 to 2025-02-06 00:00:00+00:00
Data columns (total 8 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   GF=F    2538 non-null   float64
 1   KE=F    2538 non-null   float64
 2   ZC=F    2538 non-null   float64
 3   ZL=F    2538 non-null   float64
 4   ZM=F    2538 non-null   float64
 5   ZR=F    2538 non-null   float64
 6   ZS=F    2538 non-null   float64
 7   ZW=F    2538 non-null   float64
dtypes: float64(8)
memory usage: 178.5 KB
None
Ticker                         GF=F      KE=F      ZC=F      ZL=F      ZM=F  \
Date                                                                          
2015-01-05 00:00:00+00:00  0.007673  0.012483  0.025570  0.023203  0.034462   
2015-01-06 00:00:00+00:00 -0.004330  0.010350 -0.002466 -0.000306  0.004866   
2015-01-07 00:00:00+00:00  0.004219 -0.017983 -0.021842  0.008832 -0.006222   
2015-01-08 00:00:00+00:00 -0.000111 -0.019956 -0.005060  0.018029 -0.019732   
2015-01-09 00:00:00+00:00 -0.014284 -0.012001  0.015104 -0.001192  0.006896   

Ticker                         ZR=F      ZS=F      ZW=F  
Date                                                     
2015-01-05 00:00:00+00:00  0.003537  0.036483  0.013245  
2015-01-06 00:00:00+00:00  0.002644  0.010762  0.004658  
2015-01-07 00:00:00+00:00  0.004392  0.001664 -0.020919  
2015-01-08 00:00:00+00:00 -0.010130 -0.007389 -0.021806  
2015-01-09 00:00:00+00:00  0.002653  0.006201 -0.005748  
Ticker GF=F KE=F ZC=F ZL=F ZM=F ZR=F ZS=F ZW=F
Date
2015-01-05 00:00:00+00:00 0.007673 0.012483 0.025570 0.023203 0.034462 0.003537 0.036483 0.013245
2015-01-06 00:00:00+00:00 -0.004330 0.010350 -0.002466 -0.000306 0.004866 0.002644 0.010762 0.004658
2015-01-07 00:00:00+00:00 0.004219 -0.017983 -0.021842 0.008832 -0.006222 0.004392 0.001664 -0.020919
2015-01-08 00:00:00+00:00 -0.000111 -0.019956 -0.005060 0.018029 -0.019732 -0.010130 -0.007389 -0.021806
2015-01-09 00:00:00+00:00 -0.014284 -0.012001 0.015104 -0.001192 0.006896 0.002653 0.006201 -0.005748

Plotting the time series of prices and returns side by side (2 per row)

Code
# Create a directory for plots if it doesn't exist
plots_dir = 'plots'
if not os.path.exists(plots_dir):
    os.makedirs(plots_dir)

# Plot prices
print("\nPlotting time series of prices...")
num_cols = 2  # Number of plots per row
num_plots = len(data.columns)
num_rows = (num_plots + num_cols - 1) // num_cols  # Ensure enough rows

fig, axs = plt.subplots(num_rows, num_cols, figsize=(15, 5 * num_rows))
axs = axs.flatten()

for i, col in enumerate(data.columns):
    axs[i].plot(data.index, data[col])
    axs[i].set_title(f'Price Series - {col}')
    axs[i].set_xlabel('Date')
    axs[i].set_ylabel('Price')

# Hide unused subplots
for j in range(i + 1, len(axs)):
    fig.delaxes(axs[j])

plt.tight_layout()
plt.savefig(os.path.join(plots_dir, 'price_series.png'))
plt.show()
plt.close()

# Plot returns
print("Plotting time series of returns...")
num_plots_ret = len(returns.columns)
num_rows_ret = (num_plots_ret + num_cols - 1) // num_cols

fig, axs = plt.subplots(num_rows_ret, num_cols, figsize=(15, 5 * num_rows_ret))
axs = axs.flatten()

for i, col in enumerate(returns.columns):
    axs[i].plot(returns.index, returns[col])
    axs[i].set_title(f'Return Series - {col}')
    axs[i].set_xlabel('Date')
    axs[i].set_ylabel('Log Return')

# Hide unused subplots
for j in range(i + 1, len(axs)):
    fig.delaxes(axs[j])

plt.tight_layout()
plt.savefig(os.path.join(plots_dir, 'return_series.png'))
plt.show()
plt.close()

Plotting time series of prices...

Plotting time series of returns...

Preprocessing data for LSTM time series modelling:

Code
# Function to prepare data for LSTM
def prepare_data(series, time_steps):
    X, y = [], []
    for i in range(len(series) - time_steps):
        X.append(series[i:(i + time_steps)])
        y.append(series[i + time_steps])
    return np.array(X), np.array(y)

Setting the parameters:

Code
# Defining parameters
time_steps = 5  # Number of time steps
epochs = 10  # Reduced epochs for faster execution during testing

# Dictionaries to store results
models = {}
histories = {}
mse_results = {}
scalers = {}
predictions = {}
best_params_dict = {}
residuals_analysis = {}

# Directory to save reports and graphs
report_dir = 'report'
if not os.path.exists(report_dir):
    os.makedirs(report_dir)

LSTM time series model fitting

Code
# Loop through each time series
for col in returns.columns:
    print(f"\nProcessing column: {col}")
    series = returns[col].values.reshape(-1, 1)
    
    # Check if series is empty
    if len(series) == 0:
        print(f"Series {col} is empty after preprocessing. Skipping.")
        continue
    
    print(f"Series {col} has {len(series)} data points.")
    
    # Normalizing data
    scaler = MinMaxScaler(feature_range=(0, 1))
    series_scaled = scaler.fit_transform(series)
    scalers[col] = scaler  # Storing the scaler for later inversion
    
    # Preparing data
    X, y = prepare_data(series_scaled, time_steps)
    
    # Check if X and y are non-empty
    if X.shape[0] == 0:
        print(f"Not enough data points in {col} after preparation. Skipping.")
        continue
    
    # Splitting into training and test sets
    split_index = int(0.8 * len(X))
    X_train_full, X_test = X[:split_index], X[split_index:]
    y_train_full, y_test = y[:split_index], y[split_index:]
    X_train_full = X_train_full.reshape((X_train_full.shape[0], X_train_full.shape[1], 1))
    X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))
    
    # Hyperparameter grid for Grid Search
    param_grid = {
        'neurons': [30, 50],
        'learning_rate': [0.001, 0.01],
        'activation': ['tanh', 'relu'],
        'batch_size': [32, 64]
    }
    grid = ParameterGrid(param_grid)
    
    # Initializing variables to store best results
    best_mse = float('inf')
    best_params = None
    best_model = None
    
    # Performing Grid Search
    print(f"Performing Grid Search for {col}...")
    for params in grid:
        model = Sequential()
        model.add(LSTM(params['neurons'], activation=params['activation'], input_shape=(time_steps, 1)))
        model.add(Dense(1))
        optimizer = Adam(learning_rate=params['learning_rate'])
        model.compile(optimizer=optimizer, loss='mean_squared_error')
        
        history = model.fit(
            X_train_full, y_train_full,
            validation_data=(X_test, y_test),
            epochs=epochs,
            batch_size=params['batch_size'],
            verbose=0
        )
        
        y_pred = model.predict(X_test)
        y_pred_inv = scaler.inverse_transform(y_pred)
        y_test_inv = scaler.inverse_transform(y_test.reshape(-1, 1))
        mse = mean_squared_error(y_test_inv, y_pred_inv)
        
        if mse < best_mse:
            best_mse = mse
            best_params = params
            best_model = model
            best_y_pred = y_pred
    
    best_params_dict[col] = best_params
    print(f"Best parameters for {col}: {best_params} with MSE: {best_mse}")
    
    models[col] = best_model
    predictions[col] = {'Best Model': best_y_pred}
    
    # Inverting the normalization
    y_test_inv = scaler.inverse_transform(y_test.reshape(-1, 1))
    y_pred_inv = scaler.inverse_transform(best_y_pred)
    
    # Calculating MSE
    mse_results[col] = {'Best Model': best_mse}
    
    # Visualization of results
    plt.figure(figsize=(10, 4))
    plt.plot(y_test_inv, label='Actual Value')
    plt.plot(y_pred_inv, label='Prediction')
    plt.title(f'Prediction vs Actual - {col} - Best Model')
    plt.legend()
    plt.savefig(os.path.join(report_dir, f'pred_vs_actual_{col}_Best_Model.png'))
    plt.close()
    
    # Residual Analysis
    residuals = y_test_inv - y_pred_inv
    
    # Plotting residuals
    plt.figure(figsize=(10, 4))
    plt.plot(residuals, label='Residuals')
    plt.title(f'Residuals - {col} - Best Model')
    plt.legend()
    plt.savefig(os.path.join(report_dir, f'residuals_{col}_Best_Model.png'))
    plt.close()
    
    # Ljung-Box test for autocorrelation in residuals
    lb_test = acorr_ljungbox(residuals, lags=[10], return_df=True)
    lb_pvalue = lb_test['lb_pvalue'].values[0]
    
    # Plotting residuals ACF
    fig, ax = plt.subplots(figsize=(10, 4))
    sm.graphics.tsa.plot_acf(residuals.squeeze(), lags=40, ax=ax)
    plt.title(f'Residuals Autocorrelation Function - {col}')
    plt.savefig(os.path.join(report_dir, f'acf_residuals_{col}_Best_Model.png'))
    plt.close()
    
    # Heteroscedasticity test (Breusch-Pagan Test)
    exog = sm.add_constant(best_model.predict(X_test))
    test_bp = het_breuschpagan(residuals, exog)
    bp_pvalue = test_bp[3]
    
    # Convert p-values to Python float
    lb_pvalue = float(lb_pvalue)
    bp_pvalue = float(bp_pvalue)
    
    # Saving statistical test results
    residuals_analysis[col] = {
        'residuals': residuals.flatten().tolist(),
        'ljung_box_pvalue': lb_pvalue,
        'breusch_pagan_pvalue': bp_pvalue
    }
    
    print(f"Residual Analysis for {col}:")
    print(f"Ljung-Box Test p-value: {lb_pvalue}")
    print(f"Breusch-Pagan Test p-value: {bp_pvalue}")

# Displaying final results in a table
print("\nFinal Results:")
results_table = pd.DataFrame(mse_results)
print(results_table)

Processing column: GF=F
Series GF=F has 2538 data points.
Performing Grid Search for GF=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 68ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 66ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 81ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 69ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 90ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 90ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 86ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 86ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
Best parameters for GF=F: {'activation': 'relu', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50} with MSE: 0.00010863688500248831
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 0s/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 919us/step
Residual Analysis for GF=F:
Ljung-Box Test p-value: 0.84832154632904
Breusch-Pagan Test p-value: 0.5140222285887335

Processing column: KE=F
Series KE=F has 2538 data points.
Performing Grid Search for KE=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 77ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 75ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 81ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 94ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 87ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 82ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 87ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 86ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 74ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
Best parameters for KE=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 30} with MSE: 0.00038255125951759764
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step
Residual Analysis for KE=F:
Ljung-Box Test p-value: 0.019409670595273834
Breusch-Pagan Test p-value: 0.9428560812228698

Processing column: ZC=F
Series ZC=F has 2538 data points.
Performing Grid Search for ZC=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 78ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 88ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 67ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 91ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 108ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step  16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 91ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 88ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 83ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 91ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 75ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 74ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
Best parameters for ZC=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50} with MSE: 0.00030382401862242235
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 21ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step 
Residual Analysis for ZC=F:
Ljung-Box Test p-value: 2.437562706640178e-08
Breusch-Pagan Test p-value: 0.5371942621682879

Processing column: ZL=F
Series ZL=F has 2538 data points.
Performing Grid Search for ZL=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 85ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 74ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 92ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 93ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 63ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 95ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 75ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 67ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 81ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
Best parameters for ZL=F: {'activation': 'relu', 'batch_size': 32, 'learning_rate': 0.001, 'neurons': 50} with MSE: 0.00036909943088687404
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 13ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step 
Residual Analysis for ZL=F:
Ljung-Box Test p-value: 0.2123773524884235
Breusch-Pagan Test p-value: 0.005194206956572999

Processing column: ZM=F
Series ZM=F has 2538 data points.
Performing Grid Search for ZM=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 85ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 76ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 72ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 78ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 75ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 66ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 94ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 46ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 1s 46ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 93ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 70ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 78ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
Best parameters for ZM=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50} with MSE: 0.0003311145925121033
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step 
Residual Analysis for ZM=F:
Ljung-Box Test p-value: 0.15055923115189182
Breusch-Pagan Test p-value: 0.30865864081407496

Processing column: ZR=F
Series ZR=F has 2538 data points.
Performing Grid Search for ZR=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 85ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 90ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 78ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 92ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 76ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 93ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 87ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 68ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
Best parameters for ZR=F: {'activation': 'tanh', 'batch_size': 64, 'learning_rate': 0.001, 'neurons': 30} with MSE: 0.08975818283360316
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step 
Residual Analysis for ZR=F:
Ljung-Box Test p-value: 8.462962086132558e-20
Breusch-Pagan Test p-value: 3.5052259181695084e-05

Processing column: ZS=F
Series ZS=F has 2538 data points.
Performing Grid Search for ZS=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 66ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 85ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 88ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 108ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step  16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 99ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 98ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 95ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 84ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
Best parameters for ZS=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50} with MSE: 0.00015261651944623757
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 14ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 943us/step
Residual Analysis for ZS=F:
Ljung-Box Test p-value: 0.11057863621707233
Breusch-Pagan Test p-value: 0.1363534385641425

Processing column: ZW=F
Series ZW=F has 2538 data points.
Performing Grid Search for ZW=F...
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 97ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 88ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 89ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 88ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 86ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 96ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 80ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 79ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
 1/16 ━━━━━━━━━━━━━━━━━━━━ 1s 94ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step 16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
Best parameters for ZW=F: {'activation': 'relu', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50} with MSE: 0.00040119556567816335
 1/16 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 906us/step
Residual Analysis for ZW=F:
Ljung-Box Test p-value: 0.027056318872086236
Breusch-Pagan Test p-value: 0.9418644044525549

Final Results:
                GF=F      KE=F      ZC=F      ZL=F      ZM=F      ZR=F  \
Best Model  0.000109  0.000383  0.000304  0.000369  0.000331  0.089758   

                ZS=F      ZW=F  
Best Model  0.000153  0.000401  

Saving the results in a table:

Code
# Saving results to a CSV file
results_table.to_csv(os.path.join(report_dir, 'mse_results_updated.csv'), index=True)

# Saving the best parameters found
with open(os.path.join(report_dir, 'best_params.json'), 'w') as f:
    json.dump(best_params_dict, f, indent=4)

# Saving the residual analysis
with open(os.path.join(report_dir, 'residuals_analysis.json'), 'w') as f:
    json.dump(residuals_analysis, f, indent=4)

Ploting the MSEs for each time series:

Code
# Report: Documenting the results
# Plotting the MSEs for each time series
for col in mse_results.keys():
    mse_series = mse_results[col]
    plt.figure(figsize=(10, 5))
    plt.bar(mse_series.keys(), mse_series.values(), color='blue')
    plt.title(f'MSE Comparison - {col}')
    plt.ylabel('MSE')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.savefig(os.path.join(report_dir, f'mse_comparison_{col}.png'))
    plt.close()

Saving system info:

Code
# End timer
end_time = datetime.now()
elapsed_time = end_time - start_time  # This is a timedelta object
print(f"Total execution time: {elapsed_time}")

# Save execution time to the report
system_info['Execution_Time_seconds'] = elapsed_time.total_seconds()  # Convert to float for JSON
with open(os.path.join(report_dir, 'system_info.json'), 'w') as f:
    json.dump(system_info, f, indent=4)
Total execution time: 0:05:04.961364

Generating an automatic final report:

Code
# Final Report: Generating a text document with the results
report_path = os.path.join(report_dir, 'final_report.txt')
with open(report_path, 'w') as report_file:
    report_file.write("Final Project Report - Forecasting Commodity Returns with LSTM\n")
    report_file.write("="*80 + "\n\n")
    
    report_file.write("1. Project Objectives:\n")
    report_file.write("Forecast future returns of a commodity portfolio using LSTM Neural Networks.\n\n")
    
    report_file.write("2. Methodology:\n")
    report_file.write("- Collecting commodity price data.\n")
    report_file.write("- Calculating logarithmic returns.\n")
    report_file.write("- Normalizing the data.\n")
    report_file.write("- Training LSTM models with different configurations.\n")
    report_file.write("- Performing grid search to optimize hyperparameters.\n")
    report_file.write("- Conducting residual analysis to identify uncaptured patterns and issues like autocorrelation or heteroscedasticity.\n\n")
    
    report_file.write("3. Results:\n")
    report_file.write(results_table.to_string())
    report_file.write("\n\n")
    
    report_file.write("4. Best Parameters Found (Grid Search):\n")
    report_file.write(json.dumps(best_params_dict, indent=4))
    report_file.write("\n\n")
    
    report_file.write("5. Residual Analysis:\n")
    for col, res in residuals_analysis.items():
        report_file.write(f"Residual Analysis for {col}:\n")
        report_file.write(f"Ljung-Box Test p-value: {res['ljung_box_pvalue']}\n")
        report_file.write(f"Breusch-Pagan Test p-value: {res['breusch_pagan_pvalue']}\n\n")
    report_file.write("\n")
    
    report_file.write("6. Conclusions:\n")
    report_file.write("The study demonstrated the importance of proper hyperparameter selection and model architecture for forecasting financial returns. Regularization techniques and the choice of activation function significantly influenced model performance. The residual analysis highlighted the need to consider autocorrelation and heteroscedasticity in modeling financial time series.\n\n")
    
    report_file.write("7. Recommendations for Future Work:\n")
    report_file.write("- Implement additional regularization techniques, such as DropConnect or Batch Normalization.\n")
    report_file.write("- Explore more advanced architectures, like GRU or bidirectional models.\n")
    report_file.write("- Increase the dataset to improve the models' generalization capacity.\n")
    report_file.write("- Use more robust cross-validation methods to assess model stability.\n")
    report_file.write("- Integrate other features, such as technical indicators or macroeconomic variables, to enrich model inputs.\n")
    report_file.write("- Consider hybrid models that combine Machine Learning techniques with traditional statistical models.\n")
    
    report_file.write("\nSystem Information and Execution Time:\n")
    report_file.write(json.dumps(system_info, indent=4))
    report_file.write("\n\n")
    
    report_file.write("End of Report.\n")

Results and Discussion

We begin reading the stored results by MSEs:

Code
# Reading the stored results

# 1. Reading the MSE results file
# Define the report directory
report_dir = 'report'

# Path to the MSE results file
mse_results_path = os.path.join(report_dir, 'mse_results_updated.csv')

# Read the CSV file
mse_results = pd.read_csv(mse_results_path, index_col=0)

# Display the DataFrame
print("\nMSE Results of the models:")
print(mse_results)

MSE Results of the models:
                GF=F      KE=F      ZC=F      ZL=F      ZM=F      ZR=F  \
Best Model  0.000109  0.000383  0.000304  0.000369  0.000331  0.089758   

                ZS=F      ZW=F  
Best Model  0.000153  0.000401  

Then we go to the best hyperparametes file for each time series univariate modelling:

Code
# 2. Reading the best parameters file
# Path to the best parameters file
best_params_path = os.path.join(report_dir, 'best_params.json')

# Read the JSON file
with open(best_params_path, 'r') as f:
    best_params = json.load(f)

# Display the best parameters
print("\nBest parameters found for each time series:")
for series, params in best_params.items():
    print(f"{series}: {params}")

Best parameters found for each time series:
GF=F: {'activation': 'relu', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50}
KE=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 30}
ZC=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50}
ZL=F: {'activation': 'relu', 'batch_size': 32, 'learning_rate': 0.001, 'neurons': 50}
ZM=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50}
ZR=F: {'activation': 'tanh', 'batch_size': 64, 'learning_rate': 0.001, 'neurons': 30}
ZS=F: {'activation': 'tanh', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50}
ZW=F: {'activation': 'relu', 'batch_size': 32, 'learning_rate': 0.01, 'neurons': 50}

And then we read the final report:

Code
# 3. Reading the final report
# Path to the final report
report_path = os.path.join(report_dir, 'final_report.txt')

# Read the report
with open(report_path, 'r') as report_file:
    report_content = report_file.read()

# Display the report
print("\nFinal Report Content:")
print(report_content)

Final Report Content:
Final Project Report - Forecasting Commodity Returns with LSTM
================================================================================

1. Project Objectives:
Forecast future returns of a commodity portfolio using LSTM Neural Networks.

2. Methodology:
- Collecting commodity price data.
- Calculating logarithmic returns.
- Normalizing the data.
- Training LSTM models with different configurations.
- Performing grid search to optimize hyperparameters.
- Conducting residual analysis to identify uncaptured patterns and issues like autocorrelation or heteroscedasticity.

3. Results:
                GF=F      KE=F      ZC=F      ZL=F      ZM=F      ZR=F      ZS=F      ZW=F
Best Model  0.000109  0.000383  0.000304  0.000369  0.000331  0.089758  0.000153  0.000401

4. Best Parameters Found (Grid Search):
{
    "GF=F": {
        "activation": "relu",
        "batch_size": 32,
        "learning_rate": 0.01,
        "neurons": 50
    },
    "KE=F": {
        "activation": "tanh",
        "batch_size": 32,
        "learning_rate": 0.01,
        "neurons": 30
    },
    "ZC=F": {
        "activation": "tanh",
        "batch_size": 32,
        "learning_rate": 0.01,
        "neurons": 50
    },
    "ZL=F": {
        "activation": "relu",
        "batch_size": 32,
        "learning_rate": 0.001,
        "neurons": 50
    },
    "ZM=F": {
        "activation": "tanh",
        "batch_size": 32,
        "learning_rate": 0.01,
        "neurons": 50
    },
    "ZR=F": {
        "activation": "tanh",
        "batch_size": 64,
        "learning_rate": 0.001,
        "neurons": 30
    },
    "ZS=F": {
        "activation": "tanh",
        "batch_size": 32,
        "learning_rate": 0.01,
        "neurons": 50
    },
    "ZW=F": {
        "activation": "relu",
        "batch_size": 32,
        "learning_rate": 0.01,
        "neurons": 50
    }
}

5. Residual Analysis:
Residual Analysis for GF=F:
Ljung-Box Test p-value: 0.84832154632904
Breusch-Pagan Test p-value: 0.5140222285887335

Residual Analysis for KE=F:
Ljung-Box Test p-value: 0.019409670595273834
Breusch-Pagan Test p-value: 0.9428560812228698

Residual Analysis for ZC=F:
Ljung-Box Test p-value: 2.437562706640178e-08
Breusch-Pagan Test p-value: 0.5371942621682879

Residual Analysis for ZL=F:
Ljung-Box Test p-value: 0.2123773524884235
Breusch-Pagan Test p-value: 0.005194206956572999

Residual Analysis for ZM=F:
Ljung-Box Test p-value: 0.15055923115189182
Breusch-Pagan Test p-value: 0.30865864081407496

Residual Analysis for ZR=F:
Ljung-Box Test p-value: 8.462962086132558e-20
Breusch-Pagan Test p-value: 3.5052259181695084e-05

Residual Analysis for ZS=F:
Ljung-Box Test p-value: 0.11057863621707233
Breusch-Pagan Test p-value: 0.1363534385641425

Residual Analysis for ZW=F:
Ljung-Box Test p-value: 0.027056318872086236
Breusch-Pagan Test p-value: 0.9418644044525549


6. Conclusions:
The study demonstrated the importance of proper hyperparameter selection and model architecture for forecasting financial returns. Regularization techniques and the choice of activation function significantly influenced model performance. The residual analysis highlighted the need to consider autocorrelation and heteroscedasticity in modeling financial time series.

7. Recommendations for Future Work:
- Implement additional regularization techniques, such as DropConnect or Batch Normalization.
- Explore more advanced architectures, like GRU or bidirectional models.
- Increase the dataset to improve the models' generalization capacity.
- Use more robust cross-validation methods to assess model stability.
- Integrate other features, such as technical indicators or macroeconomic variables, to enrich model inputs.
- Consider hybrid models that combine Machine Learning techniques with traditional statistical models.

System Information and Execution Time:
{
    "CPU_cores": 12,
    "CPU_freq_MHz": 1800.0,
    "Total_RAM_GB": 31.69,
    "Available_RAM_GB": 16.79,
    "GPU_info": "Not available",
    "Execution_Time_seconds": 304.961364
}

End of Report.

At the end we read the graphs for MSEs:

Code
# 4. Viewing the graphs
from IPython.display import Image, display

# List of time series
series_list = returns.columns

# Display MSE comparison graphs
for col in series_list:
    image_path = os.path.join(report_dir, f'mse_comparison_{col}.png')
    if os.path.exists(image_path):
        display(Image(filename=image_path))
    else:
        print(f"Graph {image_path} not found.")

# Display residuals graphs
for col in series_list:
    residuals_image_path = os.path.join(report_dir, f'residuals_{col}_Best_Model.png')
    acf_image_path = os.path.join(report_dir, f'acf_residuals_{col}_Best_Model.png')
    if os.path.exists(residuals_image_path):
        display(Image(filename=residuals_image_path))
    else:
        print(f"Graph {residuals_image_path} not found.")
    if os.path.exists(acf_image_path):
        display(Image(filename=acf_image_path))
    else:
        print(f"Graph {acf_image_path} not found.")

# End of code

Conclusions

 

 


References


Code
##| eval: false
# Total timing to compile this Quarto document

end_time = datetime.now()
time_diff = end_time - start_time

print(f"Total Quarto document compiling time: {time_diff}")
Total Quarto document compiling time: 0:05:05.435859